package com.yundasys.lab08jvm.lock;

/**
 * @author jiangta
 * @version 1.0
 * @date 2021/5/20 2:05 下午
 */
public class SyncLockTest {

    public static void main(String[] args) throws InterruptedException {
        final SyncLock syncLock = new SyncLock();
        for(int i=0;i<10;i++) {
            Thread.sleep(200);
            new Thread(new TestLock(syncLock),String.valueOf(i)).start();
        }
    }
}

class TestLock implements Runnable {
    private SyncLock lock;
    public TestLock(SyncLock lock) throws InterruptedException {
        this.lock = lock;
    }
    @Override
    public void run() {
        try {
            lock.lock();
            Thread.sleep(1000);
            System.out.println(String.format("Thread %s Completed", Thread.currentThread().getName()));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}